package com.site.user.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.jdbc.support.JdbcUtils;
import org.springframework.stereotype.Service;

import com.site.data.JPABaseDAOImpl;
import com.site.user.pojo.TbPrivilege;
import com.site.user.pojo.TbResource;

/**
 * @author      zhanjun
 * @date        2010-12-8
 * @version     1.0
 * @description 用户表服务层
 * 
 */
@Service("tbResourceService")
public class TbResourceService extends JPABaseDAOImpl<TbResource> implements ITbResourceService {

	/* (non-Javadoc)
	 * @see com.site.user.service.ITbResourceService#getResTree(java.lang.String, java.lang.String)
	 */
	@Override
	public String getResTree(String id, String idResource) {
		String jsql = "SELECT ID_RESOURCE,PARENT_ID,RES_NAME,RES_DESC,RES_TYPE,SYSTEM_ID,LEVEL,RESOURCE,ORDERBY,"
				+ "(SELECT COUNT(ID_RESOURCE) FROM tb_resource c WHERE c.parent_id=b.ID_RESOURCE) SIGN FROM tb_resource b where 1=1";
		String whereJPQL = "";
		if (StringUtils.isEmpty(id)) {
			//选系统级
			whereJPQL = " and RES_TYPE=1 and SYSTEM_ID=-1 and ID_RESOURCE='" + idResource + "'";
		} else {
			whereJPQL = " and RES_TYPE=0 and SYSTEM_ID='" + idResource + "' and PARENT_ID='" + id + "'";
		}
		String sql = jsql + whereJPQL + " order by orderby asc";

		List list = jdbcExecQuery(sql, TbResource.class);

		List<Map> jsonList = new ArrayList();
		for (int i = 0; i < list.size(); i++) {
			Map datamap = new HashMap();
			TbResource entity = (TbResource) list.get(i);
			datamap.put("id", entity.getIdResource());
			datamap.put("text", entity.getResName());
			int count = (Integer.parseInt(entity.getSign().trim()));

			if (count > 0) {
				datamap.put("state", "closed");
			}
			JSONObject jsob = new JSONObject(entity);
			datamap.put("attributes", jsob.toString());

			jsonList.add(datamap);
		}
		JSONArray jary = new JSONArray(jsonList);

		return jary.toString();

	}

	@Override
	public String getResPrivilegeTree(String idUser, String id, String idResource) {

		String jsql = "SELECT ID_RESOURCE,PARENT_ID,RES_NAME,RES_DESC,RES_TYPE,SYSTEM_ID,LEVEL,RESOURCE,ORDERBY,"
				+ "(SELECT COUNT(ID_RESOURCE) FROM tb_resource c WHERE c.parent_id=b.ID_RESOURCE) SIGN FROM tb_resource b where 1=1";
		String whereJPQL = "";
		if (StringUtils.isEmpty(id)) {
			//选系统级
			whereJPQL = " and RES_TYPE=1 and SYSTEM_ID=-1 and ID_RESOURCE='" + idResource + "'";
		} else {
			whereJPQL = " and RES_TYPE=0 and SYSTEM_ID='" + idResource + "' and PARENT_ID='" + id + "'";
		}
		String sql = jsql + whereJPQL + " order by orderby asc";

		List list = jdbcExecQuery(sql, TbResource.class);

		List<Map> jsonList = new ArrayList();
		for (int i = 0; i < list.size(); i++) {
			Map datamap = new HashMap();
			TbResource entity = (TbResource) list.get(i);
			datamap.put("id", entity.getIdResource());
			datamap.put("text", entity.getResName());
			int count = (Integer.parseInt(entity.getSign().trim()));

			if (count > 0) {
				datamap.put("state", "closed");
			}
			JSONObject jsob = new JSONObject(entity);
			datamap.put("attributes", jsob.toString());

			jsonList.add(datamap);
		}
		JSONArray jary = new JSONArray(jsonList);

		return jary.toString();

	}

	@Override
	public String getSystems(String idUser) {
		String jsql = "SELECT c from TbResource c where c.systemId=-1 order by c.orderby asc";
		List list = queryForList(jsql, TbResource.class);
		JSONArray jary = new JSONArray(list);
		return jary.toString();

	}
}
